﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Shared;
using JoltEnvironment.Sql;

namespace ClusterServer
{
    public partial class Client
    {
        #region Senders

        /// <summary>
        /// Sends an error message to client.
        /// </summary>
        /// <param name="MessageID">Error message id.</param>
        private void SendError(int MessageID)
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_ERROR);
            Packet.AddInt32(MessageID);

            Packet.Send(this.ClientSocket);
        }

        /// <summary>
        /// Sends a response to a ping.
        /// </summary>
        private void SendPong()
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_PONG);

            Packet.AddInt32(this.ClientState);
            Packet.AddInt64((long)Tick.ToClientTime(this.PacketTick));

            Packet.Send(this.ClientSocket);
        }

        /// <summary>
        /// Sends an unknown ping related packet to client.
        /// </summary>
        private void SendUnknown()
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(0x11);
            //Packet.AddInt32(0x29dd954e);
            //Packet.AddUInt32(0xdb553ace);
            //Packet.AddUInt32(0x97adb620);
            //Packet.AddInt32(0x1cc05ca6);

            Packet.AddBytes(new byte[] { 0x29, 0xdd, 0x95, 0x4e, 0xdb, 0x55, 0x3a, 0xce, 0x97, 0xad, 0xb6, 0x20, 0x1c, 0xc0, 0x5c, 0xa6 });

            Packet.Send(this.ClientSocket);
        }

        /// <summary>
        /// Sends the world server IP to client.
        /// </summary>
        /// <param name="WorldIP">World server IP.</param>
        private void SendWorldIP(string WorldIP)
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_WORLD_IP);
            Packet.AddString(WorldIP);

            Packet.Send(this.ClientSocket);
        }

        /// <summary>
        /// Sends the character list to the client.
        /// </summary>
        private void SendCharacterList()
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_CHARACTER_LIST);
            Packet.AddInt32(this.ClientState);

            if (Characters.Count < 1)
            {
                Packet.AddInt32(0);
                Packet.AddInt32(0);

                Packet.Send(this.ClientSocket);
            }
            else
            {
                Packet.AddInt32(Characters.Count);

                foreach (Character Character in Characters)
                {
                    Packet.AddInt32(Character.CharacterSlot);
                    Packet.AddInt32(1); // Is banned?

                    Packet.AddInt32(Character.MapID);

                    Packet.AddInt32(0x0B + Character.Gender);
                    Packet.AddString(Character.CharacterName);

                    Packet.AddFloat(Character.CharacterPosition.PosX);
                    Packet.AddFloat(Character.CharacterPosition.PosY);
                    Packet.AddFloat(Character.CharacterPosition.PosZ);

                    Packet.AddInt32(Character.CharacterID);

                    Packet.AddInt64(0);
                    Packet.AddInt64(0);

                    Packet.AddInt32(Character.HairID);
                    Packet.AddInt32(Character.HairColor);
                    Packet.AddInt32(Character.FaceID);
                    Packet.AddByte((byte)((char)Character.Gender));

                    Packet.AddInt32(Character.Class);
                    Packet.AddInt32(Character.Level);

                    Packet.AddInt32(0);
                    Packet.AddInt32(Character.Strength);
                    Packet.AddInt32(Character.Stamina);
                    Packet.AddInt32(Character.Dexterity);
                    Packet.AddInt32(Character.Intelligence);

                    Packet.AddInt64(0);

                    Packet.AddInt32(Character.CharacterItems.Count);
                    foreach (int ItemID in Character.CharacterItems)
                    {
                        Packet.AddInt32(ItemID);
                    }
                }

                Packet.AddInt32(Characters.Count);

                foreach (Character Character in Characters)
                {
                    if (Character.CharacterSlot != -1)
                    {
                        Packet.AddByte((byte)((char)Character.CharacterSlot));
                        Packet.AddInt64(0);
                    }
                }

                Packet.Send(this.ClientSocket);
            }
        }

        /// <summary>
        /// Sends a world-transfer confirmation to the client.
        /// </summary>
        private void SendWorldTransfer()
        {
            SendPacket Packet = new SendPacket();

            Packet.AddInt32(MessageHeaders.OUT_WORLD_TRANSFER);

            Packet.Send(this.ClientSocket);
        }

        #endregion
    }
}
